Cowplot is a package developed to work in tandem with R/ggplot2. From the cowplot cran page, “The cowplot package is a simple add-on to ggplot2. It is meant to provide a publication-ready theme for ggplot2, one that requires a minimum amount of fiddling with sizes of axis labels, plot backgrounds, etc.”
#install.packages("cowplot")
library(tidyverse); library(cowplot)
## Warning: package 'dplyr' was built under R version 3.5.1
We are using a data set that is within ggplot2 as well as a dataset collected from class responses. The data set from ggplot2 is called “msleep” and records average sleep data, taxonomy, diet information, and brain and body weight for 83 mammal species. The class data includes reported hours of sleep and body weight in kilograms.
animal <- msleep
weight <- c(74,63,112,70,55,50,75,59,93,88.64,54.4,84,68.18,130,100,79.54,77,52,79.5,68.2,57.72)
sleep<- c(6,6,6,6.5,6.5,7,7,7,7,7,7,7,7,7,7.25,7.5,7.5,8,8,8,9)
class_sleep <- data.frame(weight,sleep)
class_sleep_summary<- summarize(class_sleep,
mean.sleep = mean(sleep),
mean.weight = mean(weight))
class_sleep_summary
## mean.sleep mean.weight
## 1 7.107143 75.72286
## Now we will add our class data to the msleep dataset.
name<- "Class"
class.sleep<- cbind(class_sleep_summary,name)
class.sleep<- data.frame(class.sleep$name,class.sleep$mean.sleep,class.sleep$mean.weight)
names(class.sleep) <- c("msleep.name", "msleep.sleep_total", "msleep.bodywt")
new.msleep <- data.frame(msleep$name, msleep$sleep_total, msleep$bodywt)
new.msleep.class<- rbind(new.msleep, class.sleep)
First we will make several different plots using solely ggplot2 functions
msleep.hist <- ggplot(new.msleep.class, aes(x=msleep.sleep_total)) + geom_histogram(binwidth=1, aes(fill=..count..)) + labs(title="Mammal and Class Sleep") + xlab("Total Hours of Sleep")
msleep.hist #Creates a histogram for hours of sleep for both the msleep and the class data
p <- ggplot(animal, aes(x = order, y = sleep_total)) + theme(axis.text.x = element_text(face = "italic", angle = 90, vjust = 0.5)) + geom_point() + aes(colour = log(bodywt))
p
sleep.plot<-ggplot(msleep, aes(x=msleep$sleep_total, y=msleep$bodywt,colour=order)) + geom_point() + labs(x="Mean sleep (hours)", y= "Mean body weight (kg)")
sleep.plot
## The cowplot difference Notice that in cowplot, the default gray background and grid lines present in ggplot2 are missing. A similar design can also be achieved in ggplot using “+ theme_classic”
One unique feature of cowplot is the capacity to add images to your plots. For instance, we can add an image of an elephant to the background. To include this feature, we must first download the package “magick.” You can change the size of the background image using “scale=”
#install.packages("magick")
library(magick)
## Linking to ImageMagick 6.9.9.39
## Enabled features: cairo, fontconfig, freetype, lcms, pango, rsvg, webp
## Disabled features: fftw, ghostscript, x11
elephant <- image_read('https://github.com/Hiagha/NR5021/blob/master/elephant.png?raw=true')
elephant.background <- ggdraw() + draw_image(elephant,scale=0.8) + draw_plot(sleep.plot)
elephant.background
## Labeling You can also use cowplot to add labels to plots. The x and y axis denote where you want the label to appear on the plot.
elephant.background+draw_label("Image publically available from pngimg.com", x=0.5, y=0.9, size=12, fontface = "italic")
##Let's add a label to our histogram to denote the location of the mean class sleep total with respect to all mammal sleep totals.
msleep.hist + draw_label("(*) = Class mean sleep (7.1 hours)", x=15, y=10, size=10,fontface="plain") + draw_label("*", x=7.1,y=2.1,size=20,fontface="bold")
Say we wanted to look at the animal sleep data set without the elephant data points, which are the mammals with the two largest masses in the data set. We can create a new data set without these points and plot the Mass vs. Mean Sleep as before <<<<<<< HEAD
no.elephant.animal <- animal[-c(21,36),] #This code gets rid of rows 21 and 36 in the animal data set, corresponding to the two elephants (African and Asian)
#View(no.elephant.animal) #This code lets us view the new data table to confirm that the rows with the elephant information are eliminated
no.elephant.sleep.plot <- ggplot(no.elephant.animal, aes(x=no.elephant.animal$sleep_total, y=no.elephant.animal$bodywt,colour=order)) + geom_point() + labs(x="Mean sleep (hours)", y= "Mean body weight (kg)")
no.elephant.sleep.plot
## Combining plots Another advantage of cowplot is that it can combine plots into a grid or other arrangements. We can specify the size of each plot and where we want it to appear. For example, we can insert the smaller mammal mean sleep vs. mean body mass plot by a larger version of the same plot that excludes the elephant data points. To do this, we will again use the ggdraw() and draw_plot functions of cowplot. We will also remove the legend for the smaller plot and place the legend on the left for the larger plot to make it easier to view both graphs.
ggdraw() + draw_plot(no.elephant.sleep.plot + theme(legend.position = "left")) + draw_plot(sleep.plot + theme(legend.position = "none"), 0.6,0.5,0.4,0.4)
Cowplot also allows for multiple plots to put plotted gridwise with labels and then saved as a single object. This is very useful when getting plots ready for presentation or publication. Cowplot improves the functionality of ggsave( ) and makes it possible to save these combined plots as a single object.
side.by.side <- plot_grid(p, msleep.hist, labels = c("A", "B"), align = "hv", axis = "tb")
side.by.side
# We can also save our combined plots using ggsave(), setting appropriate height/width and DPI for whatever project you are working on.
four.plot <- plot_grid(sleep.plot, no.elephant.sleep.plot, p, msleep.hist, labels = c("A", "B", "C", "D"), align = "hv", axis = "tblr")
ggsave("fourplot.png", four.plot, width = 10, height = 12)
plots <- image_read("fourplot.png")
ggdraw() + draw_image(plots)